package com.tcreditdemo;

import android.content.Context;
import android.support.v7.widget.SwitchCompat;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by xiongzheng on 2018/10/16.
 *
 * https://www.jianshu.com/p/3ca70735364c
 *         感觉完全可以用组合控件
 *
 *
 *         这篇牛逼
 *         https://blog.csdn.net/pihailailou/article/details/78196854
 *
 *         首先这里设置上下宽高的margin是无效的，因为自己的根布局已经设置为了centervertical
 *
 */

public class SettingItemView extends RelativeLayout {

    private ImageView im;
    private TextView lefttv;
    private View buttomline;
    private View rightline;
    private TextView righttv;
    private ImageView intoim;
    private RelativeLayout item_root;
    private SwitchCompat switchCompat;

    public SettingItemView(Context context) {
        this(context,null);
    }

    public SettingItemView(Context context, AttributeSet attrs) {
        this(context, attrs,-1);
    }

    public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        //两个参数的情况外面改变inflate的高度无效
       // View view = View.inflate(context, R.layout.setting_item_view1, null);
        View view = LayoutInflater.from(context).inflate(R.layout.setting_item_view1, this, true);
       // addView(view);
        initialize();
    }


    private void initialize() {
        im = (ImageView) findViewById(R.id.im);
        intoim = (ImageView) findViewById(R.id.into_im);
        lefttv = (TextView) findViewById(R.id.left_tv);
        buttomline = (View) findViewById(R.id.buttom_line);
        rightline = (View) findViewById(R.id.right_line);
        righttv = (TextView) findViewById(R.id.right_tv);
        intoim = (ImageView) findViewById(R.id.into_im);
        item_root = (RelativeLayout) findViewById(R.id.item_root);
        switchCompat = (SwitchCompat) findViewById(R.id.switch_);
    }





    public  int dp2px( float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    //上下的margin值不起作用的原因是因为控件本身的属性是center_verticla
    //上下margin不可改变，具体原因未知，可能是这个是组合控件
    public void setmarginHetigh() {
        ViewGroup.LayoutParams layoutParams = righttv.getLayoutParams();
        MarginLayoutParams marginLayoutParams = (MarginLayoutParams) layoutParams;
        marginLayoutParams.setMargins(0,0,0,0);
        marginLayoutParams.setMargins(0,0,0,10);
        righttv.setLayoutParams(marginLayoutParams);
    }

    public void setRrightItemOnlyText(Context context){
        intoim.setVisibility(GONE);
        ViewGroup.LayoutParams layoutParams = righttv.getLayoutParams();
        MarginLayoutParams marginLayoutParams = (MarginLayoutParams) layoutParams;
        marginLayoutParams.setMargins(0,0,0,0);
        marginLayoutParams.setMargins(0,0,context.getResources().getDimensionPixelOffset(R.dimen.x20),0);
        righttv.setLayoutParams(marginLayoutParams);
    }

    public void setRightItemTvAndIm(Context context){
        righttv.setVisibility(VISIBLE);
        intoim.setVisibility(VISIBLE);
        ViewGroup.LayoutParams layoutParams = righttv.getLayoutParams();
        MarginLayoutParams marginLayoutParams = (MarginLayoutParams) layoutParams;
        marginLayoutParams.setMargins(0,0,0,0);
        marginLayoutParams.setMargins(0,0,context.getResources().getDimensionPixelOffset(R.dimen.x36),0);
        righttv.setLayoutParams(marginLayoutParams);
    }

    public void setRightItemOnlyImage(){
        righttv.setVisibility(GONE);
    }

    public void setRightItemHidden(){
        righttv.setVisibility(GONE);
        intoim.setVisibility(GONE);
    }

    public void setLeftTitle(String left){
        lefttv.setText(left);
    }

    public String getLeftTitle(){
        return lefttv.getText().toString().trim();
    }

    public void  setRightTitle(String right){
        righttv.setText(right);
    }

    public View getButtomline(){
        return buttomline;
    }

    public View getRightline(){
        return rightline;
    }

    public void setRightItemOnlyswitch(){
        righttv.setVisibility(GONE);
        intoim.setVisibility(GONE);
        switchCompat.setVisibility(VISIBLE);
    }

    public SwitchCompat getSwtichCompat(){
        return switchCompat;
    }


    public void setRightImage(int resid){
        im.setImageResource(resid);
    }

    //因为这个布局的原因，底部的线是aliparentbuttom，所以图片的高度，只是是和item条目的宽度一起增加
    public void setRightImageSize(int width,int height){
        ViewGroup.LayoutParams imParams = im.getLayoutParams();
        imParams.width=width;
        imParams.height=height;
        im.setLayoutParams(imParams);
        ViewGroup.LayoutParams itemParams = item_root.getLayoutParams();
        itemParams.height=height;
        item_root.setLayoutParams(itemParams);
    }


    public void setitemHeight(){
        ViewGroup.LayoutParams layoutParams = item_root.getLayoutParams();
        layoutParams.height=300;
        item_root.setLayoutParams(layoutParams);
    }




    public void setThisHeight(int height){
    //这个是自定义控件最外层的，就是这个继承了relativelayout的自定义控件。在别的xml文件中使用设置宽高就知道了
        //这个是最当前这个inflate的容器的宽高！！！
        ViewGroup.LayoutParams layoutParams = this.getLayoutParams();
        layoutParams.height=getResources().getDimensionPixelOffset(height);
        this.setLayoutParams(layoutParams);
    }


}
